package com.baizhi.controller;

import cn.hutool.http.HttpRequest;
import com.baizhi.entity.Guru;
import com.baizhi.entity.Menu;
import com.baizhi.service.GuruService;
import com.baizhi.serviceplus.GuruServicePlus;
import com.baizhi.utils.PoiUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;


@Controller
@ResponseBody
@RequestMapping("/guru")
public class GuruController {
    @Autowired
    private GuruService guruService;
    @Autowired
    private GuruServicePlus guruServicePlus;

    @RequestMapping("/select")
    public Map select(Integer page, Integer limit) {
        System.out.println("进入上师全查方法");
        Map select = guruService.select(page, limit);
        System.out.println(select);
        return select;
    }

    @RequestMapping("/insert")
    public Map insert(MultipartFile file, Guru guru, HttpServletRequest request) throws IOException {

        System.out.println("上师图片上传中-----");
        System.out.println(guru);
        //获取文件要上传的地址
        String realPath = request.getServletContext().getRealPath("/cmfz/img");
        //获取文件名
        String originalFilename = new Date().getTime() + "_" + file.getOriginalFilename();
        System.out.println(originalFilename);
       //存入数据库的名称
        String newFileName = "/cmfz/img" + "/" + originalFilename;
        //存入数据库
        guru.setImage(newFileName);
        System.out.println(guru);
        //文件上传到对应的地址
        file.transferTo(new File(realPath + "/" + originalFilename));
        System.out.println("-----------------------------------------");
        System.out.println(guru);
        Map map = guruServicePlus.insert(guru);
        return map;
    }

    @RequestMapping("/update")
    public Map update(MultipartFile file, Guru guru, HttpServletRequest request) throws IOException {
        if (!"".equals(file.getOriginalFilename())) {


            System.out.println(
                    "-----------------------------------------------------------"
            );
            System.out.println("上师信息修改中-----");
            System.out.println(guru);
            //获取文件要上传的地址
            String realPath = request.getServletContext().getRealPath("/cmfz/img");
            //获取文件名
            String originalFilename = new Date().getTime() + "_" + file.getOriginalFilename();
            //存入数据库的名称
            String newFileName = "/cmfz/img" + "/" + originalFilename;
            //存入数据库
            guru.setImage(newFileName);
            System.out.println(guru);
            //文件上传到对应的地址
            file.transferTo(new File(realPath + "/" + originalFilename));
        }
        System.out.println();
        Map map = guruServicePlus.update(guru);
        return map;
    }

    @RequestMapping("/delete")
    public Map delete(Integer id) {
        Map select = guruServicePlus.delete(id);
        return select;
    }

    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException, IllegalAccessException {
        Map map = guruService.select(1, 20);
        List<Guru> guruList = (List) map.get("data");
        String[] gurus = new String[]{"id", "上师名称", "图片", "上市法号","上师状态"};

        //获取到表格对象
        HSSFWorkbook hssfWorkbook = PoiUtils.out(guruList, gurus);
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("text.xls", "utf-8"));
        response.setContentType("application/vnd.ms-excel");
        // 通过文件对象写出
        hssfWorkbook.write(response.getOutputStream());


    }

    @RequestMapping("/upload")
    public Map upload(MultipartFile file) throws IOException {
        List<Guru> guruList = PoiUtils.in(file);
        Map map = new HashMap();
        try {
            guruService.insertMore(guruList);
                map.put("isOk", true);
        } catch (Exception e) {
            map.put("isOk", false);
        }
        return map;
    }
}